Module# 15: String Class                                                                  Lecture#58: StringBuffer Class

 

//   Example 58.1 : Creating StringBuffer objects

import java.lang;          /

// It is not necessary as it is by default imported in all programs.

class StringBufferDemo {

    public static void main(String args[]) {

      StringBuffer strOb1 = "First String";

      StringBuffer strOb2 = "Second String";

      StringBuffer strOb3 = strOb1 + " and " + strOb2;

      System.out.println(strOb1);

      System.out.println(strOb2);

      System.out.println(strOb3);

      StringBuffer sb1 = new StringBuffer();

      StringBuffer sb2 = new StringBuffer(strOb3);

      StringBuffer aString = new StringBuffer(“An example of string is” + sb2);

    }

}

 

// Example 58.2: Modifying strings

 

public class ModifyStringDemo{

    public static void main(String args[]){

    StringBuffer text = new StringBuffer("Data Structure "); 

    text.append(“C++");

    System.out.print(text);

    text.insert(15,“with ");

    System.out.print(text);

    text.replace(20,23,"Java");

    System.out.print(text);

    text.delete(14,19);

    }

}

 

// Example 58.3 : The reversing a string

 

public class StringReverseDemo{

  public static void main(String args[]){

    StringBuffer text=new StringBuffer("Data Structure With With Java"); 

    text.reverse();

    System.out.print(text);

  }

}

 

// Example 58.3: Merging of strings with String and StringBuffer methods

 

public class ConcatTest{ 

    public static String concatWithString() { 

        String s = "Java"; 

        for (int i=0; i<10000; i++){ 

            s = s.concat(“NPTEL“); 

        } 

        return s; 

    }

 

    public static String concatWithStringBuffer(){ 

        StringBuffer sb = new StringBuffer("Java"); 

        for (int i=0; i<10000; i++){ 

            sb.concat(“NPTEL"); 

        } 

        return sb.toString(); 

    }

 

// The driver class for the execution of two methods

 

    public static void main(String[] args){ 

        long startTime = System.currentTimeMillis(); 

        concatWithString(); 

        System.out.println("Time taken by Concating with String: “

                           +(System.currentTimeMillis()-startTime)+"ms"); 

        startTime = System.currentTimeMillis(); 

        concatWithStringBuffer(); 

        System.out.println("Time taken by Concating with  StringBuffer: “ 

                           +(System.currentTimeMillis()-startTime)+"ms"); 

    } 

}

 

// Example 58.4 : length( ) and capacity( )

 

// StringBuffer length vs. capacity.

class StringBufferDemo {

    public static void main(String args[]) {

    StringBuffer sb = new StringBuffer("Hello");

    System.out.println("buffer = " + sb);

    System.out.println("length = " + sb.length());

    System.out.println("capacity = " + sb.capacity());

    }

}

 

// Example 58.5 : charAt( ) and setCharAt( )

 

class setCharAtDemo {

    public static void main(String args[]) {

    StringBuffer sb = new StringBuffer("Hello");

    System.out.println("buffer before = " + sb);

    System.out.println("charAt(1) before = " + sb.charAt(1));

    sb.setCharAt(1, 'i');

    sb.setLength(2);

    System.out.println("buffer after = " + sb);

    System.out.println("charAt(1) after = " + sb.charAt(1));

   }

}

 

// Example 58.6 : Hash code of string objects

 

public class HashCodeTest{ 

    public static void main(String args[]){ 

        System.out.println("Hashcode test of String:"); 

        String str=“Java"; 

        System.out.println(str.hashCode()); 

        str=str+“NPTEL"; 

        System.out.println(str.hashCode()); 

  

        System.out.println("Hashcode test of StringBuffer:"); 

        StringBuffer sb=new StringBuffer(“Java"); 

        System.out.println(sb.hashCode()); 

        sb.append(“NPTEL"); 

        System.out.println(sb.hashCode()); 

    } 

}